from fastapi import APIRouter, Depends, Request
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_kylin.entity.vo.future_account_vo import (
    FutureAccountModel,
    FutureAccountPageQueryModel,
    DeleteFutureAccountModel,
)
from module_kylin.service.future_account_service import FutureAccountService
from utils.common_util import CamelCaseUtil
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

# 构建路由
futureAccountController = APIRouter(
    prefix='/kylin/futureAccount',
    dependencies=[Depends(LoginService.get_current_user)]
)


@futureAccountController.get('/list', response_model=PageResponseModel,
                             dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccount:list'))])
async def get_future_account_list(
        request: Request,
        page_query_model: FutureAccountPageQueryModel = Depends(),
        query_db: AsyncSession = Depends(get_db),
):
    """
    获取期货账户列表
    """
    account_list_result = await FutureAccountService.get_account_list_services(query_db, page_query_model, is_page=True)
    logger.info('获取期货账户列表成功')
    return ResponseUtil.success(data=account_list_result)


@futureAccountController.get('/{id}', response_model=FutureAccountModel,
                             dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccount:query'))])
async def query_detail_future_account(
        request: Request,
        id: int,
        query_db: AsyncSession = Depends(get_db),
):
    """
    获取期货账户详情
    """
    account_detail_result = await FutureAccountService.account_detail_services(query_db, id)
    logger.info(f'获取账户id为{id}的信息成功')
    return ResponseUtil.success(data=account_detail_result)


@futureAccountController.post('',
                              dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccount:add'))])
@Log(title='期货账户管理', business_type=BusinessType.INSERT)
async def add_future_account(
        request: Request,
        account: FutureAccountModel,
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
        query_db: AsyncSession = Depends(get_db),
):
    """
    新增期货账户
    """
    add_account_result = await FutureAccountService.add_account_services(query_db, account)
    logger.info(f'用户{current_user.user.user_name}新增期货账户成功')
    return ResponseUtil.success(msg=add_account_result.message)


@futureAccountController.put('',
                             dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccount:edit'))])
@Log(title='期货账户管理', business_type=BusinessType.UPDATE)
async def edit_future_account(
        request: Request,
        account: FutureAccountModel,
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
        query_db: AsyncSession = Depends(get_db),
):
    """
    编辑期货账户
    """
    edit_account_result = await FutureAccountService.edit_account_services(query_db, account)
    logger.info(f'用户{current_user.user.user_name}修改期货账户成功')
    return ResponseUtil.success(msg=edit_account_result.message)


@futureAccountController.delete('',
                                dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccount:delete'))])
@Log(title='期货账户管理', business_type=BusinessType.DELETE)
async def delete_future_account(
        request: Request,
        delete_account_model: DeleteFutureAccountModel,
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
        query_db: AsyncSession = Depends(get_db),
):
    """
    删除期货账户
    """
    delete_account_result = await FutureAccountService.delete_account_services(query_db, delete_account_model)
    logger.info(f'用户{current_user.user.user_name}删除期货账户成功')
    return ResponseUtil.success(msg=delete_account_result.message)


@futureAccountController.get('/all/accounts',
                             dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccount:query'))])
async def get_all_future_accounts(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
):
    """
    获取所有期货账户（不分页）
    """
    all_accounts = await FutureAccountService.get_all_accounts(query_db)
    logger.info('获取所有期货账户成功')
    return ResponseUtil.success(
        data=[FutureAccountModel(**CamelCaseUtil.transform_result(account)) for account in all_accounts])
